package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.domain.YddArticle;
import com.itheima.youdianda.common.Result;
import com.itheima.youdianda.domain.YddCate;
import com.itheima.youdianda.dto.qiantaiDto.YddArticlesDto;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddCateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pk
 * @date 2022/09/07/9:20
 */
@RestController
@RequestMapping("/ydd_article")
public class ArticleController {

    @Autowired
    private YddArticleService articleService;
    @Autowired
    private YddCateService cateService;

    /**
     * 文章列表-分页
     * @param page
     * @param limit
     * @param start_time
     * @param end_time
     * @param article
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "articleCache",keyGenerator = "commonKeyGenerator")
    public Result list(Integer page, Integer limit, Long start_time, Long end_time, YddArticle article){
        //构造分页对象
        Page pageInfo = new Page(page, limit);
        //条件构造器
        LambdaQueryWrapper<YddArticle> lqw = new LambdaQueryWrapper<>();
        //根据标题查询
        lqw.like(article.getTitle()!=null,YddArticle::getTitle,article.getTitle());
        //根据分类查询
        lqw.eq(article.getCateid()!=null,YddArticle::getCateid,article.getCateid());
        //根据开始和结束时间查询
        lqw.between(start_time!=null&&end_time!=null,YddArticle::getCreateDate,start_time,end_time);
        articleService.page(pageInfo,lqw);
        //创建map集合容器
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",pageInfo.getTotal());
        map.put("totalPages",pageInfo.getPages());
        map.put("pageSize",limit);
        map.put("currentPage",page);
        List<YddArticle> records = pageInfo.getRecords();
        List<YddArticlesDto> articlesDtos = records.stream().map((item) -> {
            YddArticlesDto articlesDto = new YddArticlesDto();
            BeanUtils.copyProperties(item, articlesDto);
            YddCate cate = cateService.getById(item.getCateid());
            if (cate != null) {
                articlesDto.setCatename(cate.getCatename());
            }
            return articlesDto;
        }).collect(Collectors.toList());
        map.put("data",articlesDtos);


        return Result.ok(map);
    }


    /**
     * 文章删除
     * @param id
     * @return
     */

    @GetMapping("/del")
    @CacheEvict(value = "articleCache",allEntries = true)
    public Result del(Integer id){

        articleService.removeById(id);
        return Result.ok("文章删除成功");
    }

    /**
     * 文章详情
     * @return
     */
    @GetMapping("/rowInfo")
    @Cacheable(value = "articleCache",key = "#id")
    public Result rowInfo(Integer id){

        YddArticle article = articleService.getById(id);
        return Result.ok(article);

    }


    /**
     * 文章添加
     * @param session
     * @param article
     * @return
     */
    @PostMapping("/add")
    @CacheEvict(value = "articleCache",allEntries = true)
    public Result add(HttpSession session, @RequestBody YddArticle article){
        Integer userId = (Integer) session.getAttribute("userId");
        article.setUserId(userId);
        articleService.save(article);

        return Result.ok(article);

    }

    /**
     * 文章修改
     * @return
     */
    @PostMapping("/update")
    @CacheEvict(value = "articleCache",allEntries = true)
    public Result update(@RequestBody YddArticle article){
        articleService.updateById(article);
        return Result.ok(article);

    }


}
